19. While and For Loops

Python vs. C++ While

Below you'll see an example of a Python while loop compared with a C++ while loop. They look quite similar!

The example starts with an integer 15 in the elapsed_time variable. With each iteration, the integer is reduced by 1. Once the elapsed_time reaches zero, the program leaves the while loop.

A generic while statement looks like this:

while (<some criteria>) {
    statement_1;
    statement_2;
    statement_3;
    ....etc
}

Python vs. C++ For loops

For loop syntax is very similar in Python and C++ as well.

This following example is like the while loop except the count variable increases instead of decreases (this does not necessarily need to be the case, we just did it that way here).

One thing to note is how Python iterates through the i variable versus how C++ does the iteration.

For python the iterator was defined here:

i in range(0, elapsed_time)

Python's range() function generates a list of numbers, which in this case would be

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Then Python assign each of these values in turn to the i variable until reaching the end of the list.

For C++, the iteration happens in this line of code:

(int i = 0; i < elapsed_time; i++) 

First you declare the variable i and assigned a value (in this case zero). The for loop then checks if

i < elapsed_time

If true, then the code block is run and then i increases by one. The code i++ is equivalent to saying i=i+1.

When

i = 14

that will be the last time that the code block runs. The code checks that 14 is less than 15, runs the code block and increases i to 15. Then the code checks if 15 is less than 15. Since that is false, the for loop does not run again.

Playground - For Loops

Use this playground to program for loops. The comments have a suggestion to get you started, and you can compare your code with the solution in solution.cpp.

Start Quiz:

#include <iostream>

int main() {
    
    //TODO: Use this as a playground to write a for loop and if statements
    // in the same program.
    
    // For example, write a for loop that iterates from 0 to 80.
    // If the iterator is greater than or equal to 0 but less than 10, 
    //      output the phrase 'slow'
    // If the iterator is between 10 inclusive
    //      and less than 30, output the phrase 'medium'
    // If the iterator is between 30 inclusive and 70, output the phrase 'fast'
    // If the iterator is greater than 70 inclusive, output the phrase 'too fast'
    
    return 0;
}
#include <iostream>

int main() {
    
    for (int i = 0; i < 80; i++) {
        if (i < 10) {
            std::cout << "slow" << std::endl;
        }
        else if (i < 30) {
            std::cout << "medium" << std::endl;
        }
        else if (i < 70) {
            std::cout << "fast" << std::endl;
        }
        else {
            std::cout << "too fast" << std::endl; 
        }
        
    }
    
    
    return 0;
}

One item to note in the above playground is that C++ considers something enclosed in single quotes ('a') to be a char, while double quotes ("fast") is a string.